package com.centaline.androidsalesblog.widgets.dropdown;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.model.businissobject.DropBo;

import java.util.ArrayList;

/**
 * 租售-房型
 * Created by guilin on 16/1/13.
 */
public class PostRoomDrop extends AbsDrop {

    private RelativeLayout rl_parent;
    private ListView listView;
    private Button btn_confirm;
    private DropCheckAdapter dropCheckAdapter;
    private SparseBooleanArray sparseArray = new SparseBooleanArray();

    public PostRoomDrop(View anchor, Context context) {
        super(anchor, context);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_drop_room, null);
        rl_parent = (RelativeLayout) view.findViewById(R.id.rl_parent);
        rl_parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        listView = (ListView) view.findViewById(R.id.listView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if ("0".equals(arrayList.get(position).getValue())) {
                    sparseArray.clear();
                    dropCheckAdapter.setSelectPos(position);
                    dropComplete(false, 40);
                } else {
                    dropCheckAdapter.select(position);
                }
            }
        });
        btn_confirm = (Button) view.findViewById(R.id.btn_confirm);
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final ArrayList<DropBo> result = new ArrayList<>();
                final int size = sparseArray.size();
                for (int i = 0; i < size; i++) {
                    if (sparseArray.valueAt(i)) {
                        result.add(arrayList.get(sparseArray.indexOfKey(i)));
                    }
                }
                final int resultSize = result.size();
                if (resultSize == 0) {
                    dropCheckAdapter.setSelectPos(0);
                    dropComplete(false, 40);
                } else {
                    dropComplete(false, 40, result.toArray(new DropBo[resultSize]));
                }
            }
        });

        dropCheckAdapter = new DropCheckAdapter();
        listView.setAdapter(dropCheckAdapter);
        initPopWindow(view);
    }

    @Override
    public void init(ArrayList<DropBo> dropBos) {
        arrayList.clear();
        arrayList.addAll(dropBos);
        dropCheckAdapter.setData(arrayList, sparseArray);
        dropCheckAdapter.setSelectPos(0, false);
    }

    @Override
    public void onDismiss() {

    }
}
